package com.zzyl.nursing.controller.member;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.PageBean;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.domain.TimeBean;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.service.ReservationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 预约信息Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags = "预约信息相关接口")
public class MemberReservationController extends BaseController {
    @Autowired
    private ReservationService reservationService;

    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public R<Integer> getCancelledReservationCount() {
        Integer count = reservationService.cancelledCount();
        return R.ok(count);
    }

    /**
     * 查询每个时间段剩余预约次数
     *
     * @param time
     * @return
     */
    @GetMapping("/countByTime")
    @ApiOperation("查询每个时间段剩余预约次数")
    public AjaxResult getCountByTime(Long time) {
        List<TimeBean> list = reservationService.getRemainingCountsByDate(time);
        return success(list);
//        Integer count = reservationService.getCountByTime(time);
//        return success(count);
    }

    /**
     * 新增预约
     */
    @PostMapping
    @ApiOperation("新增预约")
    public AjaxResult addReservation(@RequestBody ReservationDto dto) {
        reservationService.addReservation(dto);
        return success();
    }

    /**
     * 分页查询预约
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询预约")
    public AjaxResult page(@RequestParam(defaultValue = "1") Integer pageNum,
                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                        Integer  status) {
        startPage();
        List<Reservation> list = reservationService.selectReservationList(status);
        PageBean<Reservation> pageBean = new PageBean<>();
        pageBean.setRows(list);
        pageBean.setTotal(list.size());
        return success( pageBean);
    }

    /**
     * 取消预约
     */
    @PutMapping("/{id}/cancel")
    @ApiOperation("取消预约")
    public AjaxResult cancelReservation(@PathVariable Long id) {
        reservationService.cancelReservation(id);
        return success();
    }
}